/*   1:    */ package org.jetbrains.jps.model.java.impl;
/*   2:    */ 
/*   3:    */

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.Bitness;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jps.model.java.JdkVersionDetector;
import org.jetbrains.jps.model.java.JdkVersionDetector.ActionRunner;
import org.jetbrains.jps.model.java.JdkVersionDetector.JdkVersionInfo;
import org.jetbrains.jps.service.SharedThreadPool;

import java.io.*;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicReference;

/*   4:    */
/*   5:    */
/*   6:    */
/*   7:    */
/*   8:    */
/*   9:    */
/*  10:    */
/*  11:    */
/*  12:    */
/*  13:    */
/*  14:    */
/*  15:    */
/*  16:    */
/*  17:    */ 
/*  18:    */ public class JdkVersionDetectorImpl
/*  19:    */   extends JdkVersionDetector
/*  20:    */ {
/*  21: 32 */   private static final Logger a = Logger.getInstance("#com.intellij.openapi.projectRoots.impl.SdkVersionUtil");
/*  22: 33 */   private static final JdkVersionDetector.ActionRunner b = new JdkVersionDetector.ActionRunner()
/*  23:    */   {
/*  24:    */     public Future<?> run(Runnable paramAnonymousRunnable)
/*  25:    */     {
/*  26: 36 */       return SharedThreadPool.getInstance().executeOnPooledThread(paramAnonymousRunnable);
/*  27:    */     }
/*  28:    */   };
/*  29:    */   
/*  30:    */   @Nullable
/*  31:    */   public String detectJdkVersion(String paramString)
/*  32:    */   {
/*  33: 43 */     return detectJdkVersion(paramString, b);
/*  34:    */   }
/*  35:    */   
/*  36:    */   @Nullable
/*  37:    */   public String detectJdkVersion(String paramString, JdkVersionDetector.ActionRunner paramActionRunner)
/*  38:    */   {
/*  39: 48 */     JdkVersionDetector.JdkVersionInfo localJdkVersionInfo = detectJdkVersionInfo(paramString, paramActionRunner);
/*  40: 49 */     if (localJdkVersionInfo != null) {
/*  41: 50 */       return localJdkVersionInfo.getVersion();
/*  42:    */     }
/*  43: 52 */     return null;
/*  44:    */   }
/*  45:    */   
/*  46:    */   public JdkVersionDetector.JdkVersionInfo detectJdkVersionInfo(String paramString)
/*  47:    */   {
/*  48: 57 */     return detectJdkVersionInfo(paramString, b);
/*  49:    */   }
/*  50:    */   
/*  51:    */   public JdkVersionDetector.JdkVersionInfo detectJdkVersionInfo(String paramString, JdkVersionDetector.ActionRunner paramActionRunner)
/*  52:    */   {
/*  53: 62 */     String[] arrayOfString = { paramString + File.separator + "bin" + File.separator + "java", "-version" };
/*  54: 63 */     return a(paramString, arrayOfString, null, paramActionRunner);
/*  55:    */   }
/*  56:    */   
/*  57:    */   public String readVersionFromProcessOutput(String paramString1, String[] paramArrayOfString, String paramString2, JdkVersionDetector.ActionRunner paramActionRunner)
/*  58:    */   {
/*  59: 68 */     JdkVersionDetector.JdkVersionInfo localJdkVersionInfo = a(paramString1, paramArrayOfString, paramString2, paramActionRunner);
/*  60: 69 */     if (localJdkVersionInfo != null) {
/*  61: 70 */       return localJdkVersionInfo.getVersion();
/*  62:    */     }
/*  63: 72 */     return null;
/*  64:    */   }
/*  65:    */   
/*  66:    */   private static JdkVersionDetector.JdkVersionInfo a(String paramString1, String[] paramArrayOfString, String paramString2, JdkVersionDetector.ActionRunner paramActionRunner)
/*  67:    */   {
/*  68: 76 */     if ((paramString1 == null) || (!new File(paramString1).exists())) {
/*  69: 77 */       return null;
/*  70:    */     }
/*  71:    */     try
/*  72:    */     {
/*  73: 81 */       Process localProcess = Runtime.getRuntime().exec(paramArrayOfString);
/*  74: 82 */       VersionParsingThread localVersionParsingThread = new VersionParsingThread(localProcess.getErrorStream(), paramString2);
/*  75: 83 */       Future localFuture = paramActionRunner.run(localVersionParsingThread);
/*  76: 84 */       ReadStreamThread localReadStreamThread = new ReadStreamThread(localProcess.getInputStream());
/*  77: 85 */       paramActionRunner.run(localReadStreamThread);
/*  78:    */       try
/*  79:    */       {
/*  80:    */         try
/*  81:    */         {
/*  82: 89 */           localProcess.waitFor();
/*  83:    */         }
/*  84:    */         catch (InterruptedException localInterruptedException)
/*  85:    */         {
/*  86: 92 */           a.info(localInterruptedException);
/*  87: 93 */           localProcess.destroy();
/*  88:    */         }
/*  89:    */         try
/*  90:    */         {
/*  91: 98 */           localFuture.get();
/*  92:    */         }
/*  93:    */         catch (Exception localException1)
/*  94:    */         {
/*  95:101 */           a.info(localException1);
/*  96:    */         }
/*  97:104 */         str = localVersionParsingThread.getVersion();
/*  98:    */       }
/*  99:    */       finally
/* 100:    */       {
/* 101:    */         try
/* 102:    */         {
/* 103: 98 */           localFuture.get();
/* 104:    */         }
/* 105:    */         catch (Exception localException2)
/* 106:    */         {
/* 107:101 */           a.info(localException2);
/* 108:    */         }
/* 109:    */       }
/* 110:    */       String str;
/* 111:105 */       if (str != null) {
/* 112:106 */         return new JdkVersionDetector.JdkVersionInfo(str, localVersionParsingThread.getBitness());
/* 113:    */       }
/* 114:    */     }
/* 115:    */     catch (IOException localIOException)
/* 116:    */     {
/* 117:110 */       a.info(localIOException);
/* 118:    */     }
/* 119:112 */     return null;
/* 120:    */   }
/* 121:    */   
/* 122:    */   public static class ReadStreamThread
/* 123:    */     implements Runnable
/* 124:    */   {
/* 125:    */     private final InputStream a;
/* 126:    */     
/* 127:    */     protected ReadStreamThread(InputStream paramInputStream)
/* 128:    */     {
/* 129:119 */       this.a = paramInputStream;
/* 130:    */     }
/* 131:    */     
/* 132:    */     public void run()
/* 133:    */     {
/* 134:    */       try
/* 135:    */       {
/* 136:    */         for (;;)
/* 137:    */         {
/* 138:125 */           int i = this.a.read();
/* 139:126 */           if (i == -1) {
/* 140:    */             break;
/* 141:    */           }
/* 142:    */         }
/* 143:    */       }
/* 144:    */       catch (IOException localIOException)
/* 145:    */       {
/* 146:130 */         JdkVersionDetectorImpl.a.info(localIOException);
/* 147:    */       }
/* 148:    */     }
/* 149:    */   }
/* 150:    */   
/* 151:    */   public static class VersionParsingThread
/* 152:    */     implements Runnable
/* 153:    */   {
/* 154:    */     private Reader a;
/* 155:    */     private final InputStream b;
/* 156:138 */     private boolean c = false;
/* 157:    */     private final String d;
/* 158:141 */     private final AtomicReference<String> e = new AtomicReference();
/* 159:142 */     private final AtomicReference<Bitness> f = new AtomicReference(Bitness.x32);
/* 160:    */     private static final String g = "version";
/* 161:    */     private static final String h = "64-Bit";
/* 162:    */     
/* 163:    */     protected VersionParsingThread(InputStream paramInputStream, String paramString)
/* 164:    */     {
/* 165:147 */       this.b = paramInputStream;
/* 166:148 */       this.d = (paramString != null ? paramString : "version");
/* 167:    */     }
/* 168:    */     
/* 169:    */     Bitness getBitness()
/* 170:    */     {
/* 171:152 */       return (Bitness)this.f.get();
/* 172:    */     }
/* 173:    */     
/* 174:    */     String getVersion()
/* 175:    */     {
/* 176:156 */       return (String)this.e.get();
/* 177:    */     }
/* 178:    */     
/* 179:    */     public void run()
/* 180:    */     {
/* 181:    */       try
/* 182:    */       {
/* 183:161 */         this.a = new InputStreamReader(this.b);
/* 184:    */         for (;;)
/* 185:    */         {
/* 186:163 */           String str = a();
/* 187:164 */           if (str == null) {
/* 188:    */             return;
/* 189:    */           }
/* 190:165 */           if (str.contains(this.d)) {
/* 191:166 */             this.e.set(str);
/* 192:    */           }
/* 193:168 */           if (str.contains("64-Bit")) {
/* 194:169 */             this.f.set(Bitness.x64);
/* 195:    */           }
/* 196:    */         }
/* 197:    */         return;
/* 198:    */       }
/* 199:    */       catch (IOException localIOException1)
/* 200:    */       {
/* 201:174 */         JdkVersionDetectorImpl.a.info(localIOException1);
/* 202:    */       }
/* 203:    */       finally
/* 204:    */       {
/* 205:177 */         if (this.a != null) {
/* 206:    */           try
/* 207:    */           {
/* 208:179 */             this.a.close();
/* 209:    */           }
/* 210:    */           catch (IOException localIOException4)
/* 211:    */           {
/* 212:182 */             JdkVersionDetectorImpl.a.info(localIOException4);
/* 213:    */           }
/* 214:    */         }
/* 215:    */       }
/* 216:    */     }
/* 217:    */     
/* 218:    */     private String a()
/* 219:    */       throws IOException
/* 220:    */     {
/* 221:189 */       int i = 1;
/* 222:190 */       StringBuilder localStringBuilder = new StringBuilder();
/* 223:    */       for (;;)
/* 224:    */       {
/* 225:192 */         int j = this.a.read();
/* 226:193 */         if (j == -1) {
/* 227:    */           break;
/* 228:    */         }
/* 229:194 */         i = 0;
/* 230:195 */         if (j == 10)
/* 231:    */         {
/* 232:196 */           if (!this.c) {
/* 233:    */             break;
/* 234:    */           }
/* 235:197 */           this.c = false;
/* 236:    */         }
/* 237:    */         else
/* 238:    */         {
/* 239:202 */           if (j == 13)
/* 240:    */           {
/* 241:203 */             this.c = true;
/* 242:204 */             break;
/* 243:    */           }
/* 244:207 */           this.c = false;
/* 245:208 */           localStringBuilder.append((char)j);
/* 246:    */         }
/* 247:    */       }
/* 248:211 */       if (i != 0) {
/* 249:211 */         return null;
/* 250:    */       }
/* 251:212 */       String str = localStringBuilder.toString();
/* 252:    */       
/* 253:    */ 
/* 254:    */ 
/* 255:216 */       return str;
/* 256:    */     }
/* 257:    */   }
/* 258:    */ }



/* Location:           E:\idea\

 * Qualified Name:     org.jetbrains.jps.model.java.impl.JdkVersionDetectorImpl

 * JD-Core Version:    0.7.0.1

 */